/**
 * controller_teacher.js
 * 教师控制器
 * 
 * @author 	王中艺	<wangzy_smile@qq.com>
 * @date 	2017-03-03
 */
app.controller('TeacherList', function($scope, $location, $http, common){
	$scope.list 	= {};
	$scope.nPage 	= 1;
	$scope.pageHtml 	= '';
	$scope.eTeacher 	= {};
	$scope.aTeacher 	= {};
	$scope.loading 	= false;

	//加载数据
	$scope.load_data 	= function(page){
		var url 	= config.prefix_url+'/Teacher/getList?p='+page;
		$http.get(url).success(function(info){
			if(info.code != 1){
				common.dealError(info);
				return false;
			}
			$scope.list = info.data.list;
			$scope.pageHtml 	= info.data.page_html;
			$scope.nPage 	= page;
			$scope.loading 	= false;
		});
		$scope.loading 	= true;
	}

	//编辑
	$scope.edit 	= function(teacherId){
		for(var k in $scope.list){
			if($scope.list[k].id == teacherId){
				$scope.eTeacher 	= $scope.list[k];
				break;
			}
		}
		$('.modal.teacher-edit').modal('show');
	}

	//保存
	$scope.save 	= function(){
		var url 	= config.prefix_url+'/Teacher/change?';
		var param 	= {
			teacher_id : $scope.eTeacher.id,
			teacher_info : $scope.eTeacher
		}

		$http.post(url, param).success(function(info){
			if(info.code != 1){
				common.dealError(info);
				return false;
			}
			$scope.loading 	= false;
			$('.modal.teacher-edit').modal('hide');
		})
		$scope.loading 	= true;
	}

	//添加教师
	$scope.add 	= function(){
		var url 	= config.prefix_url+'/Teacher/add';
		var param 	= $scope.aTeacher;

		$http.post(url, param).success(function(info){
			if(info.code != 1){
				common.dealError(info);
				return false;
			}

			$scope.loading 	= false;
			$scope.load_data(1);
			$('.modal.teacher-add').modal('hide');
		});
		$scope.loading 	= true;
	}

	//删除教师
	$scope.delete 	= function(teacherId){
		var url 	= config.prefix_url+'/Teacher/delete?teacher_id='+teacherId;
		$http.get(url).success(function(info){
			if(info.code != 1){
				common.dealError(info);
				return false;
			}

			$scope.loading 	= false;
			$scope.load_data($scope.nPage);
		});
		$scope.loading 	= true;
	}

	$scope.load_data(1);
})